#include "Task.h"
#include <sstream>
#include <iomanip>
#include <ctime>
#include <vector>

Task::Task(int id, const string& name, time_t startTime, 
           Priority p, Category c, time_t reminder, bool reminded)
    : id_(id), name_(name), startTime_(startTime), 
      priority_(p), category_(c), 
      reminderTime_(reminder), isReminded_(reminded) {}

int Task::getId() const { return id_; }
string Task::getName() const { return name_; }
time_t Task::getStartTime() const { return startTime_; }
Task::Priority Task::getPriority() const { return priority_; }
Task::Category Task::getCategory() const { return category_; }
time_t Task::getReminderTime() const { return reminderTime_; }
bool Task::isReminded() const { return isReminded_; }

// Setters
void Task::setId(int id) { id_ = id; }
void Task::setName(const string& name) { name_ = name; }
void Task::setStartTime(time_t time) { startTime_ = time; }
void Task::setPriority(Priority p) { priority_ = p; }
void Task::setCategory(Category c) { category_ = c; }
void Task::setReminderTime(time_t time) { reminderTime_ = time; }
void Task::setReminded(bool reminded) { isReminded_ = reminded; }

string Task::serialize() const {
    ostringstream oss;
    oss << id_ << "|" << name_ << "|" << startTime_ << "|" 
        << priority_ << "|" << category_ << "|" 
        << reminderTime_ << "|" << isReminded_;
    return oss.str();
}

Task Task::deserialize(const string& data) {
    istringstream iss(data);
    string token;
    vector<string> tokens;
    
    while (getline(iss, token, '|')) {
        tokens.push_back(token);
    }
    
    if (tokens.size() != 7) {
        throw runtime_error("无效的任务数据格式");
    }
    
    return Task(
        stoi(tokens[0]),        // id
        tokens[1],              // name
        stol(tokens[2]),        // startTime
        static_cast<Priority>(stoi(tokens[3])), // priority
        static_cast<Category>(stoi(tokens[4])), // category
        stol(tokens[5]),        // reminderTime
        stoi(tokens[6])         // isReminded
    );
}
